const mongoose = require("mongoose");
const UserProblem = require("../models/UserProModel");

exports.addUserProblem = async (req, res) => {
  try {
    const newUserProblem = await UserProblem.create(req.body);
    res.status(201).json({
      status: "success",
      data: newUserProblem,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};

exports.getUserProblem = async (req, res) => {
  try {
    const UserProblemData = await UserProblem.find();
    res.status(201).json({
      status: "success",
      data: UserProblemData[0],
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};

exports.getUserProblemById = async (req, res) => {
  let id = req.params.id;
  try {
    const UserProblemData = await UserProblem.findOne({ _id: id });
    res.status(201).json({
      status: "success",
      data: UserProblemData,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};

exports.updateUserProblem = async (req, res) => {
  // console.log("更新---", req.body.problem);
  try {
    const UserProblemData = await UserProblem.updateOne(
      { _id: req.body._id },
      req.body
    );
    res.status(201).json({
      status: "success",
      data: UserProblemData,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};
